set(RIME_SOURCE_DIR ${PROJECT_SOURCE_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# work around CMake build issues on macOS
aux_source_directory(. rime_plugins_src)

unset(plugins_objs)
unset(plugins_deps)
unset(plugins_modules)
file(GLOB plugin_files "*")
foreach(file ${plugin_files})
  if (IS_DIRECTORY ${file})
    message(STATUS "Found plugin: ${file}")
    unset(plugin_name)
    unset(plugin_objs)
    unset(plugin_deps)
    unset(plugin_modules)
    add_subdirectory(${file})
    if(BUILD_MERGED_PLUGINS)
      set(plugins_objs ${plugins_objs} ${plugin_objs})
      set(plugins_deps ${plugins_deps} ${plugin_deps})
      set(plugins_modules ${plugins_modules} ${plugin_modules})
    else()
      message(STATUS "Plugin ${plugin_name} provides modules: ${plugin_modules}")
      add_library(${plugin_name} ${rime_plugins_src} ${plugin_objs})
      target_link_libraries(${plugin_name} ${plugin_deps})
      if(XCODE_VERSION)
	set_target_properties(${plugin_name} PROPERTIES INSTALL_NAME_DIR "@rpath")
      endif(XCODE_VERSION)
      install(TARGETS ${plugin_name} DESTINATION ${LIB_INSTALL_DIR})
    endif()
  endif()
endforeach(file)

set(rime_plugins_objs ${plugins_objs} PARENT_SCOPE)
set(rime_plugins_deps ${plugins_deps} PARENT_SCOPE)
set(rime_plugins_modules ${plugins_modules} PARENT_SCOPE)
